//
// Level.cpp for bomberman in /home/lefebv_w//workspace/cpp/bomberman/googleCode/level
// 
// Made by damien lefebvre
// Login   <lefebv_w@epitech.net>
// 
// Started on  Sat May 11 17:06:54 2013 damien lefebvre
// Last update Wed May 29 02:33:18 2013 damien lefebvre
//

#include		"ALevel.hh"

ALevel::ALevel(void)
  : m_startMap(0.f, 0.f, 0.f)
{}

ALevel::~ALevel(void)
{
  unload();
}

void ALevel::initialize(void)
{
  // opengl context
  window_.create();

  // camera
  m_camera.initialize(m_startMap, m_size_x, m_size_y, m_sizeBox);

  std::cout << "----- LOADING ASSETS -----" << std::endl;
  std::cout << "loading background" << std::endl;

  // map
  for (unsigned int i = 0; i < m_map.size(); i++)
    for (unsigned int j = 0; j < m_map[0].size(); j++)
      m_map[i][j].initialize();

  std::cout << "loading objects" << std::endl;
  // objects
  std::list<AObject*>::iterator itb = this->m_objects.begin();
  for (; itb != this->m_objects.end(); ++itb)
    (*itb)->initialize();
  
  std::cout << "assets loaded" << std::endl;
}

void ALevel::update(void)
{
  // map
  for (unsigned int i = 0; i < m_map.size(); i++)
    for (unsigned int j = 0; j < m_map[i].size(); j++)
      m_map[i][j].update(gameClock_, input_);

  // objects
  std::list<AObject*>::iterator itb = this->m_objects.begin();
  for (; itb != this->m_objects.end(); ++itb)
    (*itb)->update(gameClock_, input_);
  
  // camera
  m_camera.update(gameClock_, input_);
}

void ALevel::draw(void)
{
  // clean buffers
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  //glClearColor(0.74f, 0.84f, 95.0f, 1.0f);
  //glClearColor(1.f, 1.f, 1.f, 1.0f);
  glClearColor(0.f, 0.f, 0.f, 0.f);
  glClearDepth(1.0f);

  // map
  for (unsigned int i = 0; i < m_map.size(); i++)
    for (unsigned int j = 0; j < m_map[i].size(); j++)
      m_map[i][j].draw();

  // objects
  std::list<AObject*>::iterator itb = this->m_objects.begin();
  for (; itb != this->m_objects.end(); ++itb)
    (*itb)->draw();

  // display
  this->window_.display();
}

void ALevel::unload(void)
{
  std::list<AObject*>::iterator itb = this->m_objects.begin();
  //for (; itb != this->m_objects.end(); ++itb)
    //    delete (*itb);

  for (unsigned int i = 0; i < m_map.size(); i++)
    m_map[i].clear();
  m_map.clear();
}
